package com.toune.movie.mvp.model;

import android.util.Log;

import com.toune.movie.base.Api;
import com.toune.movie.base.myUtil.NoHttpCallBack;
import com.toune.movie.base.myUtil.RequstUtils;
import com.toune.movie.base.myUtil.ThreadPoolManager;
import com.toune.movie.mvp.contract.SearchContract;
import com.toune.movie.mvp.presenter.SearchPresenter;
import com.toune.movie.vo.SearchVo;
import com.toune.movie.vo.VarietyVo;
import com.vondear.rxtool.RxThreadPoolTool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 */

public class SearchModel implements SearchContract.Model {

    SearchPresenter movieMainPresenter;

    public SearchModel(SearchPresenter movieMainPresenter) {
        this.movieMainPresenter = movieMainPresenter;
    }

    @Override
    public void getVos(final String html) {
        getHtml(html);
    }

    private void getHtml(final String html) {
//        RequstUtils.GET(Api.SearchUrl + html, false, new NoHttpCallBack<String>(String.class) {
//            @Override
//            protected void getSucceed(final String json) {
//                new RxThreadPoolTool(RxThreadPoolTool.Type.CachedThread, 1).execute(new Runnable() {
//                    @Override
//                    public void run() {
//                        paseHtml(json);
//                    }
//                });
//            }
//
//            @Override
//            protected void getFailed(String json) {
//
//            }
//        });
        ThreadPoolManager.getInstatnce().createThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                paseHtml(Api.SearchUrl + html);
            }
        });
    }


    private void paseHtml(String html) {
        try {
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(html).get();
            Elements divs = doc.getElementsByClass("item0");
            List<SearchVo> varietyVoList = new ArrayList<>();
            for (int i = 0; i < divs.size(); i++) {
                SearchVo searchVo = new SearchVo();
                Element element = divs.get(i).getElementsByTag("a").get(0);
                String detailUrl = Api.BaseUrl52 + element.attr("href");
                searchVo.setDetailUrl(detailUrl);
                String title = element.attr("title");
                searchVo.setTitle(title);
                Elements img = element.getElementsByTag("img");
                if (img.size() > 0) {
                    String imgUrl = img.get(0).attr("src");
                    searchVo.setImgURL(imgUrl);
                }
                String tag = divs.get(i).getElementsByTag("b").text();
                searchVo.setTag(tag);
                varietyVoList.add(searchVo);
            }
            movieMainPresenter.setVos(varietyVoList);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }
}
